package com.hcloud.v1.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hcloud.v1.admin.entity.User;
import com.hcloud.v1.admin.mapper.UserMapper;
import com.hcloud.v1.admin.service.AccountService;
import com.hcloud.v1.common.constant.UserConstant;

/**
 * 后台用户管理的Service.
 * 
 * <pre>
 * 后台用户管理的Service.
 * </pre>
 * 
 * @author lisheng
 * @since 1.0
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {

	@Autowired
	private UserMapper userMapper;

	/**
	 * 实现接口里的登录方法
	 * 
	 * <pre>
	 * 实现接口里的登录方法
	 * </pre>
	 * 
	 * @see com.AccountService.spirit.service.IAccountService#login(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public User login(String userName, String password) {
		User user = userMapper.getUser(userName);
		if (user == null) {
			throw new RuntimeException("帐号不存在");
		}
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		if (!encoder.matches(password, user.getPassword())) {
			throw new RuntimeException("密码不正确");
		}
		return user;
	}
}
